package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.bo.SysBaseDictTypeBo;
import com.zp.system.entity.vo.SysBaseDictTypeVo;
import com.zp.system.service.ISysBaseDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 字典类型控制器
 * 前端访问路由地址为:/system/sysBaseDictType
 *
 * @author zhang peng
 * @date 2023-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysBaseDictType")
public class SysBaseDictTypeController extends BaseController {

    private final ISysBaseDictTypeService iSysBaseDictTypeService;

    /**
     * 查询字典类型列表 (分页)
     */
    @SaCheckPermission("system:sysBaseDictType:list")
    @GetMapping("/selectPage")
    public TableDataInfo<SysBaseDictTypeVo> selectPage (SysBaseDictTypeBo bo, PageQuery pageQuery) {
        return iSysBaseDictTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询字典类型列表 (不分页)
     */
    @SaCheckPermission("system:sysBaseDictType:list")
    @GetMapping("/list")
    public R<List<SysBaseDictTypeVo>> list (SysBaseDictTypeBo bo) {
        List<SysBaseDictTypeVo> list = iSysBaseDictTypeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出字典类型列表
     */
    @SaCheckPermission("system:sysBaseDictType:export")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (SysBaseDictTypeBo bo, HttpServletResponse response) {
        List<SysBaseDictTypeVo> list = iSysBaseDictTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "字典类型", SysBaseDictTypeVo.class, response);
    }

    /**
     * 获取字典类型详细信息
     *
     * @param baseDictId 主键
     */
    @SaCheckPermission("system:sysBaseDictType:query")
    @GetMapping("/{baseDictId}")
    public R<SysBaseDictTypeVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String baseDictId) {
        return R.ok(iSysBaseDictTypeService.queryById(baseDictId));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:sysBaseDictType:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody SysBaseDictTypeBo bo) {
        return toAjax(iSysBaseDictTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:sysBaseDictType:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysBaseDictTypeBo bo) {
        return toAjax(iSysBaseDictTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除字典类型
     *
     * @param baseDictIds 主键串
     */
    @SaCheckPermission("system:sysBaseDictType:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseDictIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] baseDictIds) {
        return toAjax(iSysBaseDictTypeService.deleteWithValidByIds(Arrays.asList(baseDictIds), true) ? 1 : 0);
    }
}
